﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace LastoRevival.Website
{
	public class LoginState
	{		
		public static Guid Token
		{
			get
			{
				var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
				try
				{
					var ticket = FormsAuthentication.Decrypt(cookie.Value);
					return new Guid(ticket.Name);
				}
				catch
				{
					// handles a null or expired cookie
					return Guid.Empty;
				}
			}
		}

		public static void Login(Guid loginToken, DateTime authenticatedUntil)
		{
			var value = loginToken.ToString();

			var ticket = new FormsAuthenticationTicket(1, value, DateTime.Now, authenticatedUntil, true, string.Empty);
			
			// encrypt the ticket
			var encTicket = FormsAuthentication.Encrypt(ticket);

			// create the cookie
			HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

		}

		public static void Logout()
		{
			FormsAuthentication.SignOut();
		}

	}
}